package cn.t.dynamic.switcher.configuration;

import cn.t.dynamic.switcher.aspect.DynamicSwitchAspect;
import cn.t.dynamic.switcher.router.DynamicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DynamicSwitcherConfiguration {

    public static final String DYNAMIC_SOURCE_BEAN_NAME = "dynamicDataSource";

    /**
     * 创建切面Bean
     * @return
     */
    @Bean
    public DynamicSwitchAspect dynamicSwitchAspect() {
        return new DynamicSwitchAspect();
    }

    /**
     * 创建后处理Bean
     * @return
     */
    @Bean
    public DynamicContextAware dynamicContextAware() {
        return new DynamicContextAware();
    }

    /**
     * 动态数据源上下文-内部应该包含一个数据源key-数据源对象的键值对Map<br/>
     * mybatis中直接注入这个bean即可
     * @return
     */
    @Bean(name = DYNAMIC_SOURCE_BEAN_NAME)
    public DynamicDataSource dynamicDataSource() {
        return new DynamicDataSource();
    }
}